package com.hb.trade.api.routine.controller.center.sys;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.sys.dto.ConsoleItemDTO;
import com.hb.trade.api.routine.util.CommonUtil;
import com.hb.trade.client.project.SectionClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Version(1.0)
@Api(tags = {"操作台按钮控制接口"})
@RequestMapping("/{version}")
@RestController
public class ApiConsoleItemController {

    protected final SectionClient sectionClient;

    public ApiConsoleItemController(SectionClient sectionClient) {
        this.sectionClient = sectionClient;
    }

    @ApiOperation(value = "查询所有操作台按钮(前端调试用)", notes = "查询所有操作台按钮(前端调试用)")
    @PostMapping("/console/items/all")
    public ResponseDto itemsAll() {
        Message<List<JSONObject>> message = sectionClient.itemsAll();
        if (message.isSuccess()) {
            JSONArray consoleItemTree = CommonUtil.getConsoleItemTree(message.getData());
            return ResultUtil.msg(true, consoleItemTree);
        }
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "根据指定标段的状态获取操作台按钮信息", notes = "根据指定标段的状态获取操作台按钮信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段包id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "roleSign", value = "当前角色类型", dataTypeClass = Integer.class)
    })
    @PostMapping("/console/items/sectionId")
    public ResponseDto itemsSectionId(@RequestBody ConsoleItemDTO consoleItemDto) {
        // 设置当前用户所属角色
        Message<List<JSONObject>> message = sectionClient.itemsSectionId(JSONUtil.parseObj(consoleItemDto));
        if (message.isSuccess()) {
            JSONArray consoleItemTree = CommonUtil.getConsoleItemTree(message.getData());
            return ResultUtil.msg(true, consoleItemTree);
        }
        return ResultUtil.msg(message);
    }
}
